<?php

class MyItems_Model extends Model {

    public function __construct() {
        parent::__construct();
    }
    
    public function getAllCategories()
    {
        $arr = $this -> db -> select('SELECT * FROM category');
        return json_encode($arr);
    }

    public function findCategoryName( $id)
    {
        $arr = $this -> db -> select("SELECT * FROM category WHERE id=".$id);
        return isset($arr[0]) ? $arr[0] : null;
    }

    public function getCategorySchema()
    {
        Session::init();
        $data = Session::get('add_item');
        if ( $data != null){
            $category = $this->findCategoryName( $data['categoryId']);
            return $this->db->select("SHOW COLUMNS FROM c_".$category['name']);
        }
       
    }
    
    public function submit_step1()
    {
        $form = new Form();
        try {
            $form -> post('categoryId') 
                    ->val('digit') 
                  -> post('name') 
                    ->val('alnum')
                  -> post('price')
                  -> post('overview')
                  -> submit();
            $data = $form -> fetch();
        } catch (Exception $e) {
            return $e -> getMessage();
        }
        Session::init();
        $data['idUser'] = Session::get('user')['idUser'];
        Session::set('add_item', $data);
      /*  $data['idUser'] = Session::get('user')['idUser'];
        $result['idItem'] = $this->db->insert('item', $data);
        $result['categoryId'] = $data['categoryId'];
        return $result;*/
    }

    public function submit_step2()
    {
        $fields = $this->getCategorySchema();
        $form = new Form();
        try {
            foreach ($fields as $field) {
                if ( $field['Field'] != 'item_id')
                    $form->post($field['Field']);
            }

            $form->submit();
            $data = $form -> fetch();
        } catch (Exception $e) {
            return $e -> getMessage();
        }
        Session::init();
        $data['item_id'] = $this->db->insert('item', Session::get('add_item'));
        $table = $this->findCategoryName(Session::get('add_item')['categoryId']);
        $this->db->insert('c_'.$table['name'], $data);
        Session::set('extra_info', $data);
    }

    public function uploadImage() {
        Session::init();
        $id = Session::get('add_item')['item_id'];
        if ($_FILES["fieldname"]["error"] <= 0)
            if (file_exists("public/images/items/" . $id . "_" . $_FILES["fieldname"]["name"])) {
                echo $_FILES["fieldname"]["name"] . " already exists. ";
            } else {
                move_uploaded_file($_FILES["fieldname"]["tmp_name"], "public/images/items/" . $id . "_" . $_FILES["fieldname"]["name"]);
                echo "Stored in: " . "upload/" . $_FILES["fieldname"]["name"];
            }

        $data = array('idItem' => $id,
            'fileName' => $id . "_" . $_FILES["fieldname"]["name"]);
        $this->db->insert('image', $data);
    }


}